<?php
/**
 * @description : show plan details with third-parties prod.
 * @author      : stephen.mo <stephen.mo@achievo.com>
 * @date        : Dec 15,2010
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set("dismplay_errors","on");

//getting parameters.
$lang             = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
$telco_name       = (isset($_REQUEST['pg'])) ? $_REQUEST['pg'] : NULL;
$call_back        = (isset($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : NULL;
$msisdn           = (isset($_REQUEST['msisdn']) && !empty($_REQUEST['msisdn'])) ? trim($_REQUEST['msisdn']) : NULL;
$tvbauth          = (isset($_REQUEST['tvbauth']) && !empty($_REQUEST['tvbauth'])) ? $_REQUEST['tvbauth'] : NULL;
$nw               = (isset($_REQUEST['nw']) && !empty($_REQUEST['nw'])) ? $_REQUEST['nw'] : NULL;

//setting path.
$base_server_path = OAuthConfig::OAuthDomainUrl($_SERVER);
$page_reset_url   = OAuthConfig::OAuthGetRestUrl($_SERVER);
$jqtouch_url      = $base_server_path.OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE;
$ajax_url         = $base_server_path.OAuthConfig::DEFAULT_AJAX_URL;
$quick_signin_url = $base_server_path.OAuthConfig::DEFAULT_QUICKLY_LOGIN;
$message_url      = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;
$confirm_url      = NULL;
$network_type     = (empty($msisdn) || (isset($_REQUEST['type']) && isset($_REQUEST['m']))) ? OAuthModelConfig::OAUTH_USING_NETWORK_TYPE_WIFI : OAuthModelConfig::OAUTH_USING_NETWORK_TYPE_3G;
$network_type     = ($telco_name==OAuthModelConfig::DEFAULT_GATEWAY_OF_CSL && isset($_REQUEST['ntype']) && !empty($_REQUEST['ntype'])) ? $_REQUEST['ntype'] : $network_type;

//check crumb string 
$params       = array();
$crumb_string = NULL;
$client_ip    = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$params['clientIp'] = $client_ip;
if (isset($_REQUEST['pFlag']) && ($_REQUEST['pFlag']=='NOT')) {
    $crumb_str = (isset($_REQUEST['crumbS'])) ? $_REQUEST['crumbS'] : NULL;
    $params['exT'] = (isset($_REQUEST['exT'])) ? $_REQUEST['exT'] : 0;
    $params['randN'] = (isset($_REQUEST['randN'])) ? $_REQUEST['randN'] : NULL;
    $crumb_string = OAuthUtils::genCrumbString($params,1);

}
else if ($rec_tvbauth) {
    $crumb_str   = (isset($_REQUEST['crumbS'])) ? $_REQUEST['crumbS'] : NULL;
    $params['exT'] = (isset($_REQUEST['exT'])) ? $_REQUEST['exT'] : 0;
    $params['randN']= (isset($_REQUEST['randN'])) ? $_REQUEST['randN'] : NULL;
    $params['loginId'] = $login_id;
    $crumb_string = OAuthUtils::genCrumbString($params,2);
}
else {
    $crumb_str = OAuthModelConfig::M_PAGE_OF_ERROR;
}

//if not match then show error message and try to go to start page. 
if (($crumb_string != $crumb_str) || (isset($params['exT']) && (time() > $params['exT']))) {
    $other_msg = array('call_back'=>urlencode($call_back));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1030,1035),$other_msg);
    exit;
}

$_crumb_pflag  = (isset($_REQUEST['pFlag']) && ($_REQUEST['pFlag'] == 'NOT')) ? '&pFlag='.$_REQUEST['pFlag'] : NULL;
$_crumb_string = $_crumb_pflag.'&exT='.$params['exT'].'&randN='.$params['randN'].'&crumbS='.$crumb_str;

//check member has logined?
$rec_tvbauth = (!empty($tvbauth)) ? OAuthUtils::chkTvbAuth($tvbauth) : FALSE;
if (!$rec_tvbauth) {
    $confirm_url = $quick_signin_url.'?lang='.$lang.'&pg='.$telco_name.'&redirect_to='.urlencode($call_back);
}
else {
    $nw = (empty($nw)) ? base64_encode($network_type) : $nw;
    $confirm_url = $base_server_path."/$telco_name/".OAuthModelConfig::$telco_payment_proc.'?lang='.$lang.'&pg='.$telco_name.'&tvbauth='.$tvbauth.'&nw='.$nw.'&redirect_to='.urlencode($call_back).$_crumb_string;   
}

$login_id = OAuthUtils::getLoginIdByTvbAuth($tvbauth);
$failure_page     = $base_server_path."/$telco_name/".OAuthModelConfig::$telco_payment_fail;
$success_page     = $base_server_path."/$telco_name/".OAuthModelConfig::$telco_payment_succ;
$quick_login_url  = $base_server_path.OAuthConfig::DEFAULT_QUICKLY_LOGIN.'?pg='.$telco_name.'&redirect_to='.urlencode($call_back);
$plan_details_url = $base_server_path.OAuthConfig::DEFAULT_SUPPORT_PLAN_DETAILS.'?lang='.$lang.'&pg='.$telco_name.'&tvbauth='.$tvbauth;
$error_url        = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL.'?act=request&flag=failure&pg='.$telco_name.'&lang='.$lang;
$cancel_url       = $base_server_path.OAuthConfig::DEFAULT_CMODEL_URL.'?lang='.$lang.'&call_back='.urlencode($call_back);


//check if MSISDN has subscribed myTV+
//check member's local subscribed order plan.
//$rev = OAuthUtils::chkH3gUserStatus($login_id);
$rev = OAuthUtils::chkMemberLocalStatus($login_id,$telco_name);
//if return 1,then goto payment success page,It has been auto renew successfuly.
if ($rev == 1) {
    $other_msg = array('pg'=>$telco_name,'call_back'=>urlencode($call_back));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::DEFAULT_CONST_FINISH,OAuthModelConfig::DEFAULT_CONST_SUCCESS,$lang,array(),$other_msg);
    exit;
}
//if return 2,then goto message page to show.It exists active plan.
elseif ($rev == 2) {
    $other_msg = array('pg'=>$telco_name,'call_back'=>urlencode($call_back));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::DEFAULT_CONST_EXISTS,OAuthModelConfig::DEFAULT_CONST_SUCCESS,$lang,array(),$other_msg);
    exit;
}
//goto third payment logic.
else {
    //As diffrent telco payment gate way to do diffrent logic.
    switch($telco_name) {
        case OAuthModelConfig::DEFAULT_GATEWAY_OF_H3G:
            /*{{{*/
            //check msisdn is valid or not.?
            $msisdn_time = 0;
            $msisdn_str  = NULL;
            //WiFi
            if (isset($_REQUEST['type']) && empty($msisdn) && in_array(base64_decode($_REQUEST['type']),OAuthModelConfig::$support_network_type[$telco_name])){
                $network_type = base64_decode($_REQUEST['type']);
                $tmp_m_vars   = (isset($_REQUEST['m']) && !empty($_REQUEST['m'])) ? $_REQUEST['m'] : NULL;
                $msisdn_str   = ($network_type == OAuthModelConfig::OAUTH_USING_NETWORK_TYPE_WIFI && !empty($tmp_m_vars)) ? OAuthUtils::getH3GMsisdnForWifi($tmp_m_vars) : NULL;
                $msisdn_time  = time();
            }
            
            if (!empty($msisdn)) {//3G
                $now_time = time();
                $rec = OAuthUtils::decryptMsisdn($msisdn);
                $msisdn_str  = $rec['msisdn_str'];
                $msisdn_time = $rec['msisdn_time'];
                //the expire time > 60 is invalid.
                if (($now_time-$msisdn_time) > 60) {
                    $msisdn_time = 0;
                    $msisdn_str  = NULL;
                }
            }
                
            //if invaild msisdn then redirect or check it.
            if (empty($msisdn_str) && $msisdn_time==0) {
                $ret_ip = OAuthUtils::chkIpRange($telco_name,$client_ip);
                if (-1 == (int)$ret_ip ) {
                    $other_msg = array('pg'=>$telco_name,'call_back'=>urlencode($call_back));
                    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1040),$other_msg);
                    exit;
                }

                //if return true then the network is 3G,will goto and get msisdn.
                $xml_configs  = OAuthUtils::getPaymentConfDetail($telco_name);
                $new_error_url = $error_url."&call_back=".urlencode($call_back)."&errids=".json_encode(array(1040));           
                if ($ret_ip) {//3G
                    $callback_url = $plan_details_url.'&nw='.base64_encode(OAuthModelConfig::OAUTH_USING_NETWORK_TYPE_3G).'&call_back='.$call_back.$_crumb_string; 
                    $redirect_url  = (!empty($xml_configs) && (isset($xml_configs['get_msisdn_url']))) ? $xml_configs['get_msisdn_url'].'?call_back='.urlencode($callback_url) : $new_error_url;
                    OAuthUtils::redirectTo($redirect_url);
                    exit;
                }
                else {//WiFi
                    $network_type       = OAuthModelConfig::OAUTH_USING_NETWORK_TYPE_WIFI; 
                    $mapping_third_lang = OAuthModelConfig::$mapping_third_parties_lang[$lang];
                    $reback_url         = $plan_details_url.'&type='.base64_encode($network_type).'&call_back='.$call_back.$_crumb_string;
                    $msisdn_url_path    = '?lang='.$mapping_third_lang.'&URLTo='.urlencode($reback_url);
                    $redirect_url       = (!empty($xml_configs) && (isset($xml_configs['forward_page_url']))) ? $xml_configs['forward_page_url'].$msisdn_url_path : $new_error_url;
                    OAuthUtils::redirectTo($redirect_url);
                    exit;
                }
            }
            /*}}}*/
            break;
        case OAuthModelConfig::DEFAULT_GATEWAY_OF_CSL:
            /*{{{*/
            $config_vars   = OAuthUtils::getPaymentConfDetail($telco_name);
            $landing_pages = $config_vars['landing_page'];
            $tmp_var = "url_$network_type";
            $landing_url = $landing_pages[$tmp_var];
            $language     = OAuthModelConfig::$mapping_third_parties_lang[$lang];
            //$tvbauth      = OAuthUtils::genTVBAuthString($login_id,$telco_name);
            $tvbauth = OAuthUtils::getTvbAuthByLoginId($login_id);
            if (!$tvbauth) {
                $other_msg = array('pg'=>$telco_name,'call_back'=>urlencode($call_back));
                OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1035,1043),$other_msg);
                exit;
            }
            $callback_url = $call_back;
            $redirect_url = $landing_url.'?language='.$language.'&tvbauth='.$tvbauth.'&oauth_callback='.urlencode($callback_url).'&ntype='.$network_type;
            Header("Location:$redirect_url");
            exit;
            /*}}}*/
            break;
        case OAuthModelConfig::DEFAULT_GATEWAY_OF_PCCW:
            break;
        case OAuthModelConfig::DEFAULT_GATEWAY_OF_SMARTONE:
            break;
        default:
            $other_msg = array('pg'=>$telco_name,'call_back'=>urlencode($call_back));
            OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1035,1043),$other_msg);
            exit;
    }
}

//page info vars.
$plan_detail_page             = OPI::$plan_details_page_info[$lang];
$payment_buttons              = OPI::$payment_button_info[$lang];
$msisdn                       = (empty($msisdn) && $network_type==OAuthModelConfig::OAUTH_USING_NETWORK_TYPE_WIFI) ? base64_encode($msisdn_str.'|'.$msisdn_time) : $msisdn;

//setting the form datas into array.
$form = array();
$using_real_env               = OAuthModelConfig::USING_TEMPLATE_ON_REAL_ENV;
$page                         = OAuthModelConfig::M_PAGE_OF_PLAN_DETAILS;
$form['lang']                 = $lang;
$form['title']                = $plan_detail_page['page_title'];
$form['telco_name']           = $telco_name;
//$form['hidden']['msisdn_id']  = $msisdn;
$form['hidden']               = array();
$form['button']['confirm']    = array('value'=>$payment_buttons['confirm_signin'],'link'=>$confirm_url.'&msisdn='.$msisdn);
$form['button']['cancel']     = array('value'=>$payment_buttons['cancel'],'link'=>$cancel_url);

//include thie template for plan cmodel page.
include "..".OAuthModelConfig::DEFAULT_MOBILE_TEMPLATE.OAuthModelConfig::$mobile_teplate_page[$page];

?>
